<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\ShippingTemplAttr as mShippingTemplAttr;
use Biz\model\GoodsExtendInfo as mGoodsExtendInfo;
use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;

//新疆、西藏、内蒙、甘肃、青海、港、澳、台地区不包邮的商品
$areaArr = ["110000", "120000", "130000", "140000", "150000", "210000", "220000", "230000", "310000", "320000", "330000", "340000", "350000", "360000", "370000", "410000", "420000", "430000", "440000", "450000", "460000", "500000", "510000", "520000", "530000", "610000", "640000"];
$where = [
    'type' => 1
];
$templIds = [];
$res = mShippingTemplAttr::select("*", $where);
foreach ($res as $row) {
    $area = $row['area'];
    $templArea = explode(',', $area);
    if (!array_diff($areaArr, $templArea)) {
        array_push($templIds, $row['templ_id']);
    }
}
var_dump($templIds);

$goodsIds=mGoodsExtendInfo::select(['goods_id'],['shipping_templ_id'=>$templIds]);
$goodsIds=array_column($goodsIds,'goods_id');

$list=mGoodsBasicInfo::select('*',['goods_id'=>$goodsIds]);
var_dump($list);
$title = [
    '商品id',
    '商品名称',
    '最低价',
    '最高价',
];
$time=time();
$dir = "/tmp/";
$file_path = $dir . "shippingFreeGoods" . $time. ".csv";
$fp = fopen($file_path, 'w');
fputcsv($fp, $title);
foreach ($list as $row) {
        $content = [
            $row['goods_id'],
            $row['title'],
            $row['min_price'],
            $row['max_price'],
        ];
        fputcsv($fp, $content);
}
fclose($fp);

$sendMail = [
    'liziyan@haohaozhu.com',
    'tangyiwan@haohaozhu.com'
];
\Biz\tools\Mail::send($sendMail ,"包邮商品",'包邮商品', $file_path);